/*
 * sky72310.h
 *
 *  Created on: May 29, 2013
 *      Author: Grigori
 */

#ifndef SKY72310_H_
#define SKY72310_H_
#include <gpio.h>

// in startup.c    --->    void board_init(void)  --->  Add SKY72310_Init();, #include "sky72310.h"
// in CLI.c  --->    (Find VCO)  SKY72310_CONFIG.F_VCO_MAIN = curr_val;   SKY72310_CONFIG.CRYSTAL_FREQ = curr_val;

/////////////////// union-neri meji struct-eri hertakanutyuny hakaraka

//{ SKY72310 GPIO SPI
#define SKY72310_CSSYN_PIN	AVR32_PIN_PA08
#define SKY72310_SCLK_PIN	AVR32_PIN_PA22
#define SKY72310_CDATA_PIN	AVR32_PIN_PA21
#define SKY72310_FTEST_PIN	AVR32_PIN_PA29
//}


//{ SKY72310 Registers
#define DIVIDER 					0
#define DIVIDEND_MSB 				1
#define DIVIDEND_LSB 				2
#define RF_DIVIDERS  				5
#define PH_D__CH_P_C 				6
#define PWR_DWN__MUX_OUTP_SL_CNTRL  7
#define MODULATION_CTRL				8
#define MODULATION_DATA				9

struct SKY72310_Registers
{
	union
		{
			struct
			{
				unsigned short	DATA		: 10;
				unsigned short	UNUSED		: 2;
				unsigned short	address 	: 4;
			} REG9_bits;
			unsigned short   MOD_DATA_REG9;
		} REG9;

	union
		{
			struct
			{
				unsigned short	DATA		: 10;
				unsigned short	UNUSED		: 2;
				unsigned short	address 	: 4;
			} REG8_bits;
			unsigned short   MOD_CTRL_REG8;
		} REG8;

	union
	{
		struct
		{
			unsigned short	DATA		: 10;
			unsigned short	UNUSED		: 2;
			unsigned short	address 	: 4;
		} REG7_bits;
		unsigned short   PWR_DWN_MUX_OTP_SEL_REG7;
	} REG7;

	union
	{
		struct
		{
			unsigned short	DATA		: 12;
			unsigned short	address 	: 4;
		} REG6_bits;
		unsigned short   PHASE_DETEC_CHARGE_P_CNTR_REG6;
	} REG6;

	union
	{
		struct
		{
			unsigned short	DATA		: 10;
			unsigned short	UNUSED		: 2;
			unsigned short	address 	: 4;
		} REG5_bits;
		unsigned short   REF_FREQ_DIVIDER_REG5;
	} REG5;

	union
	{
		struct
		{
			unsigned short	DATA		: 8;
			unsigned short	UNUSED		: 4;
			unsigned short	address 	: 4;
		} REG2_bits;
		unsigned short   DIVIDEND_LSB_REG2;
	} REG2;

	union
	{
		struct
		{
			unsigned short	DATA		: 10;
			unsigned short	UNUSED		: 2;
			unsigned short	address 	: 4;
		} REG1_bits;
		unsigned short   DIVIDEND_MSB_REG1;
	} REG1;

	union
	{
		struct
		{
			unsigned short	DATA		: 9;
			unsigned short	UNUSED		: 3;
			unsigned short	address 	: 4;
		} REG0_bits;
		unsigned short   DIVIDER_REG0;
	} REG0;

} SKY_REG ;
//}
//{ SKY72310 configurable data
//#define F_VCO_MAIN 902.4530 //MHz
//#define CRYSTAL_FREQ 40 //MHz




//{ SKY72310 configurable data
typedef struct
{
	double F_VCO_MAIN;
	double    CRYSTAL_FREQ;

}SKY_CONF;
SKY_CONF SKY72310_CONFIG;
//}

#define HIGH_MODE  // DELTA_EPSILON modulator MODE (10 bit or 18 bit)

#ifdef LOW_MODE
#define BIT_MODE 10
#define divider 1024
#define MAX_INTERN_REF 25 //need to correct
#endif

#ifdef HIGH_MODE
#define BIT_MODE 18
#define divider 262144
#define MAX_INTERN_REF 25
#endif


//Prototypes
void SKY72310_CSel(void);
void SKY72310_CUnsel(void);
void SKY72310_Write(unsigned short SKY_reg_address ,unsigned short data);
void SKY72310_Init(void);
double N_FRACTIONAL(void);
short N_REG(double);
short DIVIDEND(double, short);
short ROUND(double);
unsigned int SKY72310_Lock_Detect(void);    // F_TEST
int SKY72310_Set_Frequency(double, double);




#endif /* SKY72310_H_ */
